package model.tablemodel;

import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.table.DefaultTableModel;

import database.DBConnection;

public abstract class AbstractTableModel extends DefaultTableModel {

	private static final long serialVersionUID = 1L;
	protected String basicQuery = "";
	protected String orderBy = "";
	protected String whereStmt = "";

	public AbstractTableModel(Object[] colNames, int rowCount) {
		super(colNames, rowCount);
	}

	// Otvaranje upita
	public void open() throws SQLException {
		DBConnection.getConnection().setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		fillData(basicQuery + whereStmt + orderBy);
	}

	// Popunjavanje matrice podacima

	protected abstract void fillData(String sql) throws SQLException;

	public abstract void deleteRow(int index) throws SQLException;

	protected abstract int sortedInsert(String[] cols);

	public abstract int insertRow(String[] cols) throws SQLException;

	public abstract void searchRows(String[] criteria) throws SQLException;

	public abstract int editRow(String[] cols, int index) throws SQLException;
	
	public void setWhereStmt(String whereStmt){
		this.whereStmt = whereStmt;
	}
	
	protected abstract boolean checkRow(int index);

}
